package com.ttb.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DBHandler extends SQLiteOpenHelper {
	// database version
	public static final int DATABASE_VERSION = 1;
	// database name
	public static final String DATABASE_NAME = "smshelper";
	// block table name
	public static final String BLOCK_TABLE = "block_list";
	// block columns name
	public static final String BLOCK_KEY_ID = BaseColumns._ID;
	public static final String BLOCK_KEY_VALUE = "value";
	public static final String BLOCK_KEY_TYPE = "type";
	// contact table name
	public static final String CONTACT_TABLE = "contacts";
	// contact columns name
	public static final String CONTACT_KEY_ID = BaseColumns._ID;
	public static final String CONTACT_KEY_NAME = "name";
	public static final String CONTACT_KEY_PH_NO = "phone_number";
	// schedule table name
	public static final String SCHEDULE_TABLE = "schedule_list";
	// schedule columns name
	public static final String SCHEDULE_KEY_ID = BaseColumns._ID;
	public static final String SCHEDULE_KEY_PHONE_LIST = "list_phone_number";
	public static final String SCHEDULE_KEY_TRIGGER_TIME = "trigger_time";
	public static final String SCHEDULE_KEY_INTERVAL = "interval";
	public static final String SCHEDULE_KEY_MESSAGE = "message";
	// user table name
	public static final String USER_TABLE = "user";
	// user columns name
	public static final String USER_KEY_ID = BaseColumns._ID;
	public static final String USER_KEY_PASS = "password";
	//message table name
	public static final String SMS_TABLE = "message";
	//message columns name
	public static final String SMS_ID = BaseColumns._ID;
	public static final String SMS_ADDRESS = "address";
    public static final String SMS_PERSON = "person";
    public static final String SMS_DATE = "date";
    public static final String SMS_READ = "read";
    public static final String SMS_STATUS = "status";
    public static final String SMS_TYPE = "type";
    public static final String SMS_BODY = "body";
    public static final String SMS_SEEN = "seen";
	public DBHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create table block
		String CREATE_BLOCK_TABLE = "CREATE TABLE " + BLOCK_TABLE + "( "
				+ BLOCK_KEY_ID + " INTEGER PRIMARY KEY, " + BLOCK_KEY_VALUE
				+ " TEXT," + BLOCK_KEY_TYPE + " TEXT" + ");";
		Log.d("BLOCK TABLE CREATE :", CREATE_BLOCK_TABLE);
		db.execSQL(CREATE_BLOCK_TABLE);
		// create table contact
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + CONTACT_TABLE + "("
				+ CONTACT_KEY_ID + " INTEGER PRIMARY KEY ," + CONTACT_KEY_NAME
				+ " TEXT, " + CONTACT_KEY_PH_NO + " TEXT" + ");";
		Log.d("Create contact table", CREATE_CONTACTS_TABLE);
		db.execSQL(CREATE_CONTACTS_TABLE);
		// create table schedule
		String CREATE_SCHEDULE_TABLE = "CREATE TABLE " + SCHEDULE_TABLE + "( "
				+ SCHEDULE_KEY_ID + " INTEGER PRIMARY KEY, "
				+ SCHEDULE_KEY_PHONE_LIST + " TEXT," + SCHEDULE_KEY_MESSAGE
				+ " TEXT," + SCHEDULE_KEY_TRIGGER_TIME + " LONG, "
				+ SCHEDULE_KEY_INTERVAL + " TEXT" + ");";
		Log.d("SCHEDULE TABLE CREATE :", CREATE_SCHEDULE_TABLE);
		db.execSQL(CREATE_SCHEDULE_TABLE);
		// create user table
		String CREATE_USER_TABLE = "CREATE TABLE " + USER_TABLE + "( " + USER_KEY_ID
				+ " INTEGER PRIMARY KEY, " + USER_KEY_PASS + " TEXT"
				+");";
		Log.d("USER TABLE CREATE :", CREATE_USER_TABLE);
		db.execSQL(CREATE_USER_TABLE);
		//create message table
		String CREATE_SMS_TABLE = "CREATE TABLE " + SMS_TABLE + "(" 
		 +SMS_ID + " INTEGER PRIMARY KEY, " + SMS_ADDRESS + " TEXT,"
		 + SMS_PERSON + " TEXT," + SMS_DATE + " INTEGER, "
		 + SMS_READ + " INTEGER, " + SMS_STATUS + " INTEGER, "
		 + SMS_TYPE + " INTEGER, " + SMS_BODY + " TEXT,"
		 + SMS_SEEN + " INTEGER )" ;
		db.execSQL(CREATE_SMS_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// drop table if exist
		String dropSql = "DROP TABLE IF EXIST " + BLOCK_TABLE;
		db.execSQL(dropSql);
		dropSql = "DROP TABLE IF EXIST " + CONTACT_TABLE;
		db.execSQL(dropSql);
		dropSql = "DROP TABLE IF EXIST " + SCHEDULE_TABLE;
		db.execSQL(dropSql);
		dropSql = "DROP TABLE IF EXIST " + USER_TABLE;
		db.execSQL(dropSql);
		dropSql = "DROP TABLE IF EXIST " + SMS_TABLE;
		db.execSQL(dropSql);
		onCreate(db);

	}

}
